<!doctype html>
<html lang="en">
	<head>
		<title>three.js misc - lights - point + directional</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background:#fff;
				padding:0;
				margin:0;
				overflow:hidden;
				font-family:georgia;
				text-align:center;
			}
			h1 { }
			a { color:skyblue }
			canvas { pointer-events:none; z-index:10; }

			#d { text-align:center; margin:1em 0 -11.4em 0; z-index:0; position:relative; display:block }
			.button { background:#000; color:#fff; padding:0.2em 0.5em; cursor:pointer }
			.inactive { background:#999; color:#eee }
		</style>
	</head>

	<body>
		<div id="d">
			<h1>lights test</h1>

			<span id="rcanvas" class="button inactive">2d canvas renderer</span>
			<span id="rwebgl" class="button">WebGL renderer</span>
			<br/>

			<p>Best viewed in Chrome 8/9 or Firefox 4 using WebGL renderer.
			<p>Canvas renderer is very slow on anything other than Chrome.
		</div>

		<script src="../build/Three.js"></script>

		<script src="js/Stats.js"></script>

		<script>

			var SCREEN_WIDTH = window.innerWidth / 2;
			var SCREEN_HEIGHT = window.innerHeight;
			var FLOOR = -250;

			var container, stats;

			var camera, scene, canvasRenderer, webglRenderer;

			var mesh, zmesh, lightMesh, geometry;

			var directionalLight, pointLight;

			var mouseX = 0, mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			var render_canvas = 1, render_gl = 1;
			var has_gl = 0;

			var bcanvas = document.getElementById( 'rcanvas' );
			var bwebgl = document.getElementById( 'rwebgl' );

			document.addEventListener( 'mousemove', onDocumentMouseMove, false );

			init();
			animate();

			//render_canvas = !has_gl;
			bwebgl.style.display = has_gl ? 'inline' : 'none';
			bcanvas.className = render_canvas ? 'button' : 'button inactive';

			function init() {

				container = document.createElement('div');
				document.body.appendChild(container);

				scene = new THREE.Scene();

				camera = new THREE.PerspectiveCamera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
				camera.position.z = 500;

				scene.add( camera );

				// Spheres

				var geometry = new THREE.SphereGeometry( 100, 16, 8 );
				var material = new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, overdraw: true } );

				for ( var i = 0; i < 30; i ++ ) {

					mesh = new THREE.Mesh( geometry, material );
					mesh.position.x = 500 * ( Math.random() - 0.5 );
					mesh.position.y = 500 * ( Math.random() - 0.5 );
					mesh.position.z = 500 * ( Math.random() - 0.5 );
					mesh.scale.x = mesh.scale.y = mesh.scale.z = 0.25 * ( Math.random() + 0.5 );
					scene.add( mesh );

				}

				// Torus

				mesh = new THREE.Mesh( new THREE.TorusGeometry( 100, 25, 15, 30 ), new THREE.MeshLambertMaterial( { color: 0xffffff, overdraw: true } ) );
				scene.add( mesh );

				// Lights

				var ambient = new THREE.AmbientLight( 0x101010 );
				scene.add( ambient );

				directionalLight = new THREE.DirectionalLight( 0xffffff );
				directionalLight.position.set( 0, -70, 100 ).normalize();
				scene.add( directionalLight );

				pointLight = new THREE.PointLight( 0xffaa00 );
				scene.add( pointLight );

				lightMesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
				lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
				lightMesh.position = pointLight.position;
				scene.add( lightMesh );


				if ( render_canvas ) {

					canvasRenderer = new THREE.CanvasRenderer();
					canvasRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
					container.appendChild( canvasRenderer.domElement );

				}

				if ( render_gl ) {

					try {

						webglRenderer = new THREE.WebGLRenderer( { alpha: true } );
						webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
						webglRenderer.domElement.style.position = "relative";
						container.appendChild( webglRenderer.domElement );

						has_gl = 1;

					} catch (e) {

					}

				}

				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.top = '0px';
				stats.domElement.style.zIndex = 100;
				container.appendChild( stats.domElement );

				bcanvas.addEventListener("click", toggleCanvas, false);
				bwebgl.addEventListener("click", toggleWebGL, false);

			}

			function onDocumentMouseMove( event ) {

				mouseX = ( event.clientX - windowHalfX );
				mouseY = ( event.clientY - windowHalfY );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			var r = 0, counter = 0;

			function render() {

				counter == 30 ? scene.remove( directionalLight ) : counter ++;

				camera.position.x += ( mouseX - camera.position.x ) * .05;
				camera.position.y += ( - mouseY - camera.position.y ) * .05;

				camera.lookAt( scene.position );

				for ( var i = 0, l = scene.children.length; i < l; i ++ ) {

					var object = scene.children[ i ];

					if ( object instanceof THREE.Mesh ) {

						if ( i % 3 == 1 )

							object.rotation.z += 0.05;

						else if ( i % 3 == 2 )

							object.rotation.y += 0.05;

						else if ( i % 3 == 0 )

							object.rotation.x += 0.05;

					}

				}


				lightMesh.position.x = 200 * Math.cos( r );
				lightMesh.position.z = 200 * Math.sin( r );

				r += 0.1;

				if ( render_canvas ) canvasRenderer.render( scene, camera );
				if ( render_gl && has_gl ) webglRenderer.render( scene, camera );

			}

			function toggleCanvas() {

				render_canvas = !render_canvas;
				bcanvas.className = render_canvas ? "button" : "button inactive";

				render_gl = !render_canvas;
				bwebgl.className = render_gl ? "button" : "button inactive";

				if( has_gl )
					webglRenderer.domElement.style.display = render_gl ? "block" : "none";

				canvasRenderer.domElement.style.display = render_canvas ? "block" : "none";

			}

			function toggleWebGL() {

				render_gl = !render_gl;
				bwebgl.className = render_gl ? "button" : "button inactive";

				render_canvas = !render_gl;
				bcanvas.className = render_canvas ? "button" : "button inactive";

				if( has_gl )
					webglRenderer.domElement.style.display = render_gl ? "block" : "none";

				canvasRenderer.domElement.style.display = render_canvas ? "block" : "none";

			}

		</script>

	</body>
</html>
